<template>
  <div>
    <el-dialog
      :title="type == 'add' ? '新增' : '修改'"
      :visible.sync="isShow"
      width="60%"
      :before-close="close"
    >
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="基础配置" name="first">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="名称" prop="goodsname">
              <el-input v-model="ruleForm.goodsname"></el-input>
            </el-form-item>
            <el-form-item label="一级分类">
              <el-select
                v-model="ruleForm.first_cateid"
                @change="firstChange"
                placeholder="请选择一级分类"
              >
                <el-option
                  v-for="(menu, index) in firstList"
                  :key="index"
                  :label="menu.catename"
                  :value="menu.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
              <el-select
                v-model="ruleForm.second_cateid"
                placeholder="请选择二级分类"
              >
                <el-option
                  v-for="(menu, index) in secondList"
                  :key="index"
                  :label="menu.catename"
                  :value="menu.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="ruleForm.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="2">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品描述" name="second">
          <div id="aaa" style="height: 400px"></div>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor";
import { getCateListTree, getCateListByTop } from "../../apis";
export default {
  props: ["isShow", "type"],
  data() {
    return {
      // tab面板中激活的模块
      activeName: "first",
      // 一级分类的下拉数据
      firstList: [],
      // 二级分类的下拉数据
      secondList: [],
      // 表单的数据
      ruleForm: {
        rolename: "",
        menus: "",
        status: 1,
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
      // 编辑器
      editor: null,
    };
  },
  created() {
    // 发请求 先获取一级的数据
    this.getFirstlist();
  },
  mounted() {},
  methods: {
    setVal(data) {
      this.ruleForm = { ...data };
    },
    async getFirstlist() {
      let res = await getCateListTree();
      this.firstList = res.list;
    },
    close() {
      this.$emit("close");
    },
    async firstChange() {
      // 修改一级分类,此时要重新获取二级分类的下拉数据
      let res = await getCateListByTop({ pid: this.ruleForm.first_cateid });
      this.secondList = res.list;
    },
    // tab切换
    handleClick() {
      if(this.activeName == 'second' && !this.editor) {
        this.$nextTick(() => {
          this.editor = new E("#aaa");
          // 注意，先配置 height ，再执行 create()
          this.editor.create();
        });
      }
    },
    submit() {
      // 点击提交按钮
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 获取用户勾选的菜单对应的id
          // this.$refs.tree 是选择树形控件 getCheckedKeys是组件自己的方法 这里是父组件调用子组件的函数
          this.ruleForm.menus = this.$refs.tree.getCheckedKeys().join();
          // 校验成功 可以提交数据了
          let res;
          // 先判断新增还是修改
          if (this.type == "add") {
            res = await addRole(this.ruleForm);
          } else {
            res = await editRole(this.ruleForm);
          }
          if (res.code == 200) {
            this.$emit("update");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      this.close();
    },
  },
};
</script>

<style>
</style>